package com.ycode.demo.controller.convertor;

import com.ycode.demo.model.User;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * <p>Title: BookCaseMessageConverter.java </p>
 * <p>Description: </p>
 * <p>Company: Snail Game </p>
 *
 * @author Xinying Ge
 * @date: 05/08/2014
 * @time: 17:33
 */
public class BookCaseMessageConverter extends AbstractHttpMessageConverter<User> {

    public BookCaseMessageConverter() {
    }

    public BookCaseMessageConverter(MediaType supportedMediaType) {
        super(supportedMediaType);
    }

    public BookCaseMessageConverter(MediaType... supportedMediaTypes) {
        super(supportedMediaTypes);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return User.class.equals(clazz);
    }

    @Override
    protected User readInternal(Class<? extends User> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(httpInputMessage.getBody(), "UTF-8"));
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);
        return new User();
    }

    @Override
    protected void writeInternal(User books, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
      System.out.println("2222");
    }
}